﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
// thread
using System.Threading;

using System.Runtime.InteropServices;

namespace mesRUpload
{
    public class NoticeProgressBar
    {
        Form pf;
        ProgressBar pb;
        Label lbl;
        public static Thread thread;
        private static object locker = new object(); // 创建锁

        private static NoticeProgressBar m_instance; // 单例

        public static int Count { get; set; }

        public static string title { set; get; }

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetForegroundWindow();

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left; //最左坐标
            public int Top; //最上坐标
            public int Right; //最右坐标
            public int Bottom; //最下坐标
        }

        public static int getWndSize(int i)
        {
            IntPtr myPtr = GetForegroundWindow();
            RECT fx = new RECT();
            GetWindowRect(myPtr, ref fx);
            if (i == 1)
            {
                return fx.Right - fx.Left; // 窗口的宽度
            }
            if (i == 2)
            {
                return fx.Bottom - fx.Top; // 窗口的高度
            }
            if (i == 3)
            {
                return fx.Left; // x
            }
            if (i == 4)
            {
                return fx.Top; // y
            }
            return 0;
        }

        public static NoticeProgressBar getInstance()
        {
            if (m_instance == null)
            {
                m_instance = new NoticeProgressBar(false);
            }

            return m_instance;
        }

        private NoticeProgressBar()
        {
            pf = new Form();
            pf.Size = new Size(getWndSize(1), 100);
            pf.ControlBox = false;
            pf.ShowInTaskbar = true;
            pf.TopMost = true;
            pf.FormBorderStyle = FormBorderStyle.FixedSingle;
            pf.Text = title;
            pf.Location = new Point(getWndSize(3), getWndSize(4) - 110);
            pb = new ProgressBar();
            Control.CheckForIllegalCrossThreadCalls = false;
            pb.Style = ProgressBarStyle.Marquee;
            pf.Controls.Add(pb);
            pf.Padding = new Padding(5);
            pb.Dock = DockStyle.Fill;
            pf.Location = new Point(100, 200);
        }

        public NoticeProgressBar(bool isCircle)
        {
            Count = CodeCollection.DATA_ISNOTEXIST;

            pf = new Form();
            pf.Size = new Size(getWndSize(1), 100);
            pf.ControlBox = false;
            pf.ShowInTaskbar = false;
            pf.TopMost = false; ;
            pf.FormBorderStyle = FormBorderStyle.FixedSingle;
            pf.Text = title;
            pf.Location = new Point(getWndSize(3), getWndSize(4) - 110);

            if (isCircle)
            {
                pb = new ProgressBar();
                Control.CheckForIllegalCrossThreadCalls = false;
                pb.Style = ProgressBarStyle.Marquee;
                pf.Controls.Add(pb);
                pf.Padding = new Padding(5);
                pb.Dock = DockStyle.Fill;
                pf.Location = new Point(100, 200);
                //pf.StartPosition = FormStartPosition.CenterScreen;
                pf.Show();
            }
            else
            {
                pb = new ProgressBar();
                pb.Step = 1;
                pb.Maximum = 100;
                pb.Minimum = 1;
                pf.Padding = new Padding(5);
                //pb.Dock = DockStyle.Fill;
                pb.Size = new Size(getWndSize(1) - 200, 100);
                pb.Location = new Point();
                lbl = new Label();
                lbl.TextAlign = ContentAlignment.MiddleLeft;
                lbl.AutoSize = false;
                lbl.Size = new Size(180, 100);
                lbl.ForeColor = Color.Blue;
                lbl.Font = new Font("宋体", 12);
                lbl.Location = new Point(getWndSize(1) - 195, 0);
                lbl.Text = "文件加载中......";
                lbl.Update();
                pf.Controls.Add(lbl);
                pf.Controls.Add(pb);
                pf.Show();
                //pb.Value = 50;
            }
        }

        public void Show()
        {
            pf.Show();
        }

        public static void show_circle_dialog()
        {
            lock (locker)
            {
                if (thread != null && thread.IsAlive)
                {
                    MessageBox.Show("文件多次加载，请退出！");
                    try
                    {
                        thread.Abort();
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("启动加载：" + e.Message);
                        close_circle_dialog();
                    }
                    finally
                    {
                        thread = null;
                    }
                }
                else
                {
                    ThreadStart ts = new ThreadStart(NoticeProgressBar.show);
                    thread = new Thread(ts);
                    thread.Start();
                    Thread.Sleep(100);
                }
            }
        }

        public static void show()
        {
            NoticeProgressBar sp = new NoticeProgressBar(true);
            run(sp);
        }

        public static void run(object sp)
        {
            Random r = new Random();
            NoticeProgressBar spp = sp as NoticeProgressBar;
            if (Count != CodeCollection.DATA_ISNOTEXIST)
            {
                for (int i = 0; i < Count; i++)
                {
                    spp.set(i);
                    Thread.Sleep(100);
                }
            }
            else
            {
                while (true)
                {
                    spp.set(r.Next(100));
                    Thread.Sleep(100);
                }
            }
        }

        private delegate void Set(int value); // 代理

        public void set(int value)
        {
            if (pb.InvokeRequired)
            {
                Set setpos = new Set(set);
                pb.Invoke(setpos, new object[] { value });
            }
            else
            {
                pb.Value = Convert.ToInt32(value);
            }
        }

        public delegate void deg_set();

        public void close()
        {
            if (pf.InvokeRequired)
            {
                deg_set set_deg = new deg_set(close);
                pf.Invoke(set_deg);
            }
            else
            {
                pf.Close();
                pf.Dispose();
            }
        }

        public static void close_circle_dialog()
        {
            if (thread != null)
            {
                try
                {
                    thread.Abort();
                }
                catch (Exception e)
                {
                    MessageBox.Show("关闭加载：" + e.Message);
                }
                finally
                {
                    thread = null;
                }
            }
        }

        public void set(int value, string pro)
        {
            if (pb.InvokeRequired)
            {
                Set set_deg = new Set(set);
                pb.Invoke(set_deg, new object[] { value });
            }
            else
            {
                try
                {
                    pb.Value = value;
                    lbl.Text = pro;
                    lbl.Update();
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }
        }

        public void clear()
        {
            if (pb != null)
            {
                pb = null;
            }
            if (pf != null)
            {
                pf = null;
            }
            if (thread != null)
            {
                thread = null;
            }
        }

    }
}

//noticeprogressbar npb = new noticeprogressbar(false);   
//npb.set(10, "10%，完成加载【基本信息】");//完成加载基本信息
//thread.sleep(100);
//application.doevents();
//npb.set(20, "20%，完成加载【规则】");//完成加载规则
//thread.sleep(1000);
//application.doevents();
//npb.set(99, "20%，完成加载【规则】");//完成加载规则
//thread.sleep(1000);
//application.doevents();